*** REPLICATION FILES Dietrich, Milner and Slapin “From Text to Political Positions on Foreign Aid: Analysis of Aid Mentions in Party Manifestos from 1960 to 2015”
*** File 1 
*** 29 May 2020
*** JBS
*** Stata SE 15.1 

*** FIRST FILE TO RUN
*** LOADS manifesto_rawcoding.DTA -- ORIGINAL CODER DATA IN STATA FORMAT
*** Main Outputs: correlation table (appendix B), Paper Figure 1, All results in Appendix C


*** SET YOUR WORKING DIRECTORY TO THE REPLICATION FOLDER

clear all
set more off
set matsize 1000

use "input/manifesto_rawcoding.dta" , clear

* Generate a country code variable for use in later merging 
gen ccode = .
replace ccode = 2 if cty=="us"
replace ccode = 20 if cty=="can"
replace ccode = 200 if cty=="uk"
replace ccode = 205 if cty=="ire"
replace ccode = 220 if cty=="fra"
replace ccode = 255 if cty=="ger"
replace ccode = 375 if cty=="fin"
replace ccode = 380 if cty=="swe"
replace ccode = 385 if cty=="nor"
replace ccode = 390 if cty=="den"
replace ccode = 900 if cty=="aus"
replace ccode = 920 if cty=="nz"
replace ccode = 230 if cty=="spain"

* Collapse aid variable by manifesto so we get a count of aid mentions (total, pro, neg) per manifesto
collapse (sum) aid_c1 proaid_c1 negaid_c1 aid_c2 proaid_c2 negaid_c2   , by(ccode partyid year cty) 

destring partyid, replace

** Produce Correlation Table in Appendix B
** Table written to output folder --file: correls.txt
tempfile temp1
save `temp1'
file open correls using "output/correls.txt", write replace

file write correls  "cty" _tab "ccode" _tab "aidrho" _tab "proaidrho" _tab "negaidrho" _n

levelsof ccode, local(levels)

foreach l of local levels {

    keep if ccode == `l'
 	
	cor aid_c1 aid_c2 
	local aidrho = r(rho)
	gen aidrho = `aidrho'

	cor proaid_c1 proaid_c2
	local proaidrho = r(rho)
	gen proaidrho = `proaidrho'
	
	cor negaid_c1 negaid_c2
	local negaidrho = r(rho)
	gen negaidrho = `negaidrho'

	keep cty ccode aidrho proaidrho negaidrho
	
	sort cty ccode aidrho proaidrho negaidrho
	quietly by cty ccode aidrho proaidrho negaidrho:  gen dup = cond(_N==1,0,_n)
	drop if dup > 1
    
	file write correls %9s (cty) _tab %7.3f (ccode) _tab %7.3f (aidrho) _tab %7.3f (proaidrho) _tab %7.3f (negaidrho) _n
	
	use `temp1', clear	
}

file close correls

use `temp1', clear

** Merge in wordcounts (ntokens) for full manifesto and international sections
** These come from the Manifestos Project
** Keep only parties for which everything is coded (_merge==3)

** Create new variable electyr which is simply "year" for merging. 
gen electyr = year

preserve
 use "input/ntokens", replace
 sort partyid electyr
 save "input/ntokens", replace
 
restore
merge 1:1 partyid electyr using "input/ntokens"
keep if _merge==3 
drop _merge

replace NTokenFor = 0 if NToken~=. & NTokenFor==.
 
* Create variables averaging across coders (except for aidwords, which we did above)
egen aid1 = rowmean(aid_c1 aid_c2)
egen proaid1 = rowmean(proaid_c1 proaid_c2)
egen negaid1 = rowmean(negaid_c1 negaid_c2)
 
**** Create aid sentences per 1000 words of manifesto
gen aid_intl = (aid1/NTokenFor)*1000
gen aid_tot = (aid1/NToken)*1000
gen intl_tot = (NTokenFor/NToken)*1000

gen proaid_tot = (proaid1/NToken)*1000
gen negaid_tot = (negaid1/NToken)*1000


*** Examine aid sentences per manifesto sentences
gen aid_tot_sent = (aid1/NSent)

gen proaid_tot_sent = (proaid1/NSent)

** correlate aid mentions per 1000 words with aid mentions per sentences
corr aid_tot aid_tot_sent
corr proaid_tot proaid_tot_sent

* generate new year variable for cases where there are more than one election in a year
gen year1 = year

replace year1 = 1974 if year==197402 | year==197410
replace year1 = 1997 if year==199705
replace year1 = 2015 if year==201505 

******
* Create total global mentions by year

preserve

collapse aid_tot proaid_tot negaid_tot, by(year1)

rename aid_tot globalaidtot

rename proaid_tot globalproaidtot

rename negaid_tot globalnegaidtot

rename year1 year
sort year

* add a row for 2014 -- no elections anywhere 2014, only year missing
set obs `=_N+1'
replace year = 2014 if year == .  
* Use previous years global mentions if mentions are missing (due to no election held)
replace globalaidtot = globalaidtot[_n-1] if globalaidtot==. | globalaidtot==0
replace globalproaidtot = globalproaidtot[_n-1] if globalproaidtot==. 

drop if year < 1960

save "output/globalmeanaidmentions.dta" , replace



* Create Neg and Pos Aid mentions Figure in Appendix 
graph twoway (lowess globalproaidtot year, mean bwidth(.2) scheme(s1mono) xlabel(1960[10]2015) ///
		leg(off) ytitle("Aid Sentences per 1000 Words") xtitle("Year") text(.4 1968 "Positive Aid Mentions", place(e)) ///
		text(.035 1965 "Negative Aid Mentions", place(e)) title("Total Mentions of Aid over Time (Smoothed)") ) ///
		(lowess globalnegaidtot year, mean bwidth(.2))
graph export output/Globalmentionsovertime.pdf, replace


restore
*****


* generate half decade variable
gen year5 = floor((year1-1960)/5)+1
label define y5 1 "1960-1964" 2 "1965-1969" 3 "1970-1974" 4 "1975-1979" 5 "1980-1984"  ///
   6 "1985-1989" 7 "1990-1994" 8 "1995-1999" 9 "2000-2004" 10 "2005-2009" 11 "2010-2013" 12 "2014-2016"
label val year5 y5
drop if year5<1
sort partyid year
 
preserve
 
* Merge with CMP data 
use "input/cmp_election.dta" , clear
 gen year = year(edate)
 gen electyr = year
 replace electyr = 199705 if country==51 & date==199705
 replace electyr = date if country==51 & (date==197402 | date==197410 | date==201505)

 rename party partyid
  duplicates tag partyid electyr , gen(tag)
 replace electyr = date if tag~=0
 drop tag
 drop if partyid==.
 tempfile cmp
 sort partyid electyr
 save `cmp'
 
 restore
 destring partyid , replace
 *drop country
 sort partyid electyr
 merge m:1 partyid electyr using `cmp'
 
 drop if _merge==2 
 drop _merge
 
gen intposneg = per107-per109
 
gen partyidorig = partyid
replace partyid = 41113 if partyid==41111 & (electyr==1983 | electyr==1987)
replace partyid = 41221 if partyid==41222 | partyid==41223
replace partyid = 14221 if partyid==14223
replace partyid = 31621 if partyid==31625 & electyr==1993

*** IDEOLOGY
gen L = rile<0
gen R = rile>0

** Create trichotomous measure for ease of interpretation
gen ideol = cond(rile<-10,1,cond(rile>=-10 & rile<=8,2,cond(rile>=8 & rile~=.,3,.)))

save "output/File1output.dta" , replace

*** TABLES 
** We regress aid sentences per 1000 words
** Percent aid mentions ~ ideology , year and country effects
** Clustered on Party

** APPENDIX TABLE C1
* Trichotomous Ideo with country and half-decade fixed effects, robust se's clusted on party
reg aid_tot ideol i.country i.year5, cl(partyid)
regsave using "output/aidtot_reg_countryyear.dta", replace
est store tab1_r1

* pro aid
reg proaid_tot ideol i.country  i.year5, cl(partyid)
regsave using "output/proaid1_reg_countryyear.dta", replace
est store tab1_r2

* Neg aid sentences
reg negaid_tot ideol i.country i.year5, cl(partyid)
est store tab1_r3

* Pro minus neg as dep var
gen prominusnegaid = proaid_tot-negaid_tot
reg prominusnegaid ideol i.country i.year5, cl(partyid)
est store tab1_r4

estout tab1_r1 tab1_r2 tab1_r3 tab1_r4 using "output/AppTabC1.txt" , replace starlevels(* .1 ** .05 *** .01) cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) ///
  stats(N N_clust r2  , fmt( %9.0f %9.0f %9.3f %9.3f) labels(N Clusters R2 )) label   


********************************************
* APPENDIX TABLE C2
* 
reg aid_tot ideol i.country i.partyid, cl(partyid)
est store tab1a_r1

* pro aid
reg proaid_tot ideol i.country i.partyid, cl(partyid)
est store tab1a_r2

* Neg aid sentences
reg negaid_tot ideol i.country i.partyid, cl(partyid)
est store tab1a_r3

reg prominusnegaid ideol i.country i.partyid, cl(partyid)
est store tab1a_r4

estout tab1a_r1 tab1a_r2 tab1a_r3 tab1a_r4 using "output/AppTabC2.txt" , replace starlevels(* .1 ** .05 *** .01) cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) ///
  stats(N N_clust r2  , fmt( %9.0f %9.0f %9.3f %9.3f) labels(N Clusters R2 )) label   


***********
* APPENDIX TABLE C3

* With country fixed effects
reg aid_tot rile i.country i.year5, cl(partyid)
est store tab1a_r1

** Pro aid /neg aid
* pro aid
reg proaid_tot rile i.country  i.year5, cl(partyid)
est store tab1a_r2

* Neg aid sentences
reg negaid_tot rile i.country i.year5, cl(partyid)
est store tab1a_r3

reg prominusnegaid rile i.country i.year5, cl(partyid)
est store tab1a_r4

estout tab1a_r1 tab1a_r2 tab1a_r3 tab1a_r4 using "output/AppTabC3.txt" , replace starlevels(* .1 ** .05 *** .01) cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) ///
  stats(N N_clust r2  , fmt( %9.0f %9.0f %9.3f %9.3f) labels(N Clusters r2)) label   

**********************************

* Compare our measure with CMP international pos-neg
* Reported in paper footnote 7
corr prominusnegaid intposneg
local rho = round(r(rho), 0.01)

* Various visual inspections of correlation. Not presented in paper. Commented out as they take a bit of time to run
/*
twoway scatter prominusnegaid intposneg || lfit prominusnegaid intposneg, title("All Observations") ///
	ytitle("Aid Mentions (Pos-Neg)") xtitle("CMP Internationalism (Pos-Neg)") text(4.5 -13 "R = `rho'") scheme(s1mono) leg(off) name(CMPcomparison, replace)

gen intposnegtrunc = intposneg
replace intposnegtrunc =. if intposneg > 10
replace intposnegtrunc =. if intposneg < 0

corr prominusnegaid intposnegtrunc
local rho = round(r(rho), 0.01)

twoway scatter prominusnegaid intposnegtrunc || lfit prominusnegaid intposnegtrunc, text(4.75 1.75 "R = `rho'") title("Truncated CMP Data") ///
	ytitle("Aid Mentions (Pos-Neg)") xtitle("CMP Internationalism (Pos-Neg)") scheme(s1mono) leg(off) name(CMPcomparisontrunc, replace)
*/

****************************
** Create Coef Plot for Ideology
** Paper Figure 1

coefplot (tab1_r1, label(Total Aid) ) (tab1_r2, label(Pro Aid)) (tab1_r3, label(Neg Aid)) (tab1_r4, label(Pos-Neg)), ///
		  rescale(2) /// Rescale so coefs represent a shift from a left party to a right party
          text(0.65 -0.055 "Total Aid") ///
          text(0.83 -.1 "Positive Aid") ///
		  text(1.03 0.025 "Negative Aid") ///
		  text(1.23 -0.13 "Positive-Negative Aid") ///
		  keep(ideol) ysc(off) leg(off) coeflabels(ideol = "Model") xline(0) xtitle("Number of Aid Statements Per 1000 Words") scheme(s1mono) title("Effect of Ideology on Aid Mentions") subtitle("Moving from Left to Right")

graph export "output/FIGURE1coefplot.pdf", replace

** Create Figures for Appendix C- FigC1 and Figure C2
use "output/aidtot_reg_countryyear.dta", clear

gen country = "Sweden" if var == "11b.country"
replace country = "Norway" if var == "12.country"
replace country = "Denmark" if var == "13.country"
replace country = "Finland" if var == "14.country"
replace country = "France" if var == "31.country"
replace country = "Spain" if var == "33.country"
replace country = "Germany" if var == "41.country"
replace country = "UK" if var == "51.country"
replace country = "Ireland" if var == "53.country"
replace country = "USA" if var == "61.country"
replace country = "Canada" if var == "62.country"
replace country = "Australia" if var == "63.country"
replace country = "N.Zealand" if var == "64.country"


gen year5 = "1960-1964" if var == "1b.year5"
replace year5 = "1965-1969" if var == "2.year5"
replace year5 = "1970-1974" if var == "3.year5"
replace year5 = "1975-1979" if var == "4.year5"
replace year5 = "1980-1984" if var == "5.year5"
replace year5 = "1985-1989" if var == "6.year5"
replace year5 = "1990-1994" if var == "7.year5"
replace year5 = "1995-1999" if var == "8.year5"
replace year5 = "2000-2004" if var == "9.year5"
replace year5 = "2005-2009" if var == "10.year5"
replace year5 = "2010-2013" if var == "11.year5"
replace year5 = "2014-2016" if var == "12.year5"

gen consval = coef[_N]

gen countrycoef = coef+consval if country != ""

gen yearcoef = coef+consval if year5 != ""
 
graph bar (mean) countrycoef, over(country, label(angle(45)) sort(countrycoef) descending) ylabel(#4) title("Country Effects") ///
	ytitle("Aid Sentences Per 1000 Words") scheme(s1mono) leg(off) name(fig2a, replace) nodraw

graph bar (mean) yearcoef, over(year5, label(angle(45))) ylabel(#4)  title("Year Effects") ytitle("") scheme(s1mono) leg(off) ///
	name(fig2b, replace) nodraw

graph combine fig2a fig2b , title("Predicted Aid Mentions") ycommon scheme(s1mono) name(fig2pred, replace)

graph export "output/APPFigC1countryyearfixed.pdf", replace
 
 
**********
use "output/proaid1_reg_countryyear.dta", clear


gen country = "Sweden" if var == "11b.country"
replace country = "Norway" if var == "12.country"
replace country = "Denmark" if var == "13.country"
replace country = "Finland" if var == "14.country"
replace country = "France" if var == "31.country"
replace country = "Spain" if var == "33.country"
replace country = "Germany" if var == "41.country"
replace country = "UK" if var == "51.country"
replace country = "Ireland" if var == "53.country"
replace country = "USA" if var == "61.country"
replace country = "Canada" if var == "62.country"
replace country = "Australia" if var == "63.country"
replace country = "N.Zealand" if var == "64.country"


gen year5 = "1960-1964" if var == "1b.year5"
replace year5 = "1965-1969" if var == "2.year5"
replace year5 = "1970-1974" if var == "3.year5"
replace year5 = "1975-1979" if var == "4.year5"
replace year5 = "1980-1984" if var == "5.year5"
replace year5 = "1985-1989" if var == "6.year5"
replace year5 = "1990-1994" if var == "7.year5"
replace year5 = "1995-1999" if var == "8.year5"
replace year5 = "2000-2004" if var == "9.year5"
replace year5 = "2005-2009" if var == "10.year5"
replace year5 = "2010-2013" if var == "11.year5"
replace year5 = "2014-2016" if var == "12.year5"

gen consval = coef[_N]

gen countrycoef = coef+consval if country != ""

gen yearcoef = coef+consval if year5 != ""
 
graph bar (mean) countrycoef, over(country, label(angle(45)) sort(countrycoef) descending) ylabel(#4) title("Country Effects") ///
	ytitle("Positive Aid Sentences Per 1000 Words") scheme(s1mono) yscale(range(0 1)) leg(off) name(fig3a, replace) nodraw

graph bar (mean) yearcoef, over(year5, label(angle(45))) ylabel(#4)  title("Year Effects") ytitle("") scheme(s1mono) leg(off) ///
	name(fig3b, replace) nodraw

graph combine fig3a fig3b , title("Predicted Positive Aid Mentions") ycommon scheme(s1mono) name(fig2pred, replace)

graph export "output/APPFigC2countryyearfixed_proaid.pdf", replace
